package com.lxl.plugin.packagejar.pack.impl;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompileStatusNotification;
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import com.lxl.plugin.packagejar.message.Messages;
import com.lxl.plugin.packagejar.pack.Packager;
import com.lxl.plugin.packagejar.util.Util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by LXLong on 2015/2/28.
 */
public class AllPacker implements Packager, CompileStatusNotification {
    private DataContext dataContext;
    private String exportPath;
    private String exportJarName;

    public AllPacker(DataContext dataContext, String exportPath, String exportJarName) {
        this.dataContext = dataContext;
        this.exportPath = exportPath;
        this.exportJarName = exportJarName;
    }

    @Override
    public void pack() {
        Project project = CommonDataKeys.PROJECT.getData(dataContext);
        Module module = LangDataKeys.MODULE.getData(dataContext);
        Messages.clear(project);

        VirtualFile[] virtualFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
        String outPutPath = CompilerPathsEx.getModuleOutputPath(module, false);

        String jdkPath = Util.getJDKPath(project);

        StringBuilder command = new StringBuilder(jdkPath);
        command.append("jar");
        command.append(" cvf ");
        command.append(this.exportPath);
        command.append("/");
        command.append(this.exportJarName);
        for (VirtualFile virtualFile : virtualFiles) {
            PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile);
            if (psiDirectory != null) {
                command.append(" -C ");
                command.append(outPutPath);
                command.append(" ");
                PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
                command.append(psiPackage.getQualifiedName().replaceAll("\\.", "/"));
                command.append("/");
            }
//            else{
//                psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile.getParent());
//                PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
//                command.append(psiPackage.getQualifiedName().replaceAll("\\.","/"));
//                command.append("/");
//                String fileName = virtualFile.getName().substring(0, virtualFile.getName().lastIndexOf("."));
//                command.append(fileName.substring(0,fileName.lastIndexOf(".")));
//            }
        }
        Messages.info(project, command.toString());
        Process process;
        try {
            process = Runtime.getRuntime().exec(command.toString());
            BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String str;
            while ((str = stream.readLine()) != null) {
                Messages.info(project, str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finished(boolean b, int error, int i1, CompileContext compileContext) {
        if (error == 0) {
            pack();
        } else {
            Project project = CommonDataKeys.PROJECT.getData(dataContext);
            Messages.info(project, "compile error");
        }
    }
}
